package com.ruoyi.project.controller;

import java.util.*;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.project.domain.RiskType;
import com.ruoyi.project.service.IRiskTypeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 风险类别Controller
 * 
 * @author liboyu
 * @date 2024-05-28
 */
@RestController
@RequestMapping("/project/riskType")
public class RiskTypeController extends BaseController
{
    @Autowired
    private IRiskTypeService riskTypeService;

    /**
     * 查询风险类别列表
     */
    @PreAuthorize("@ss.hasPermi('project:riskType:list')")
    @GetMapping("/list")
    public TableDataInfo list(RiskType riskType)
    {
//        startPage();
        List<RiskType> list = riskTypeService.selectRiskTypeList(riskType);
        return getDataTable(list);
    }

    /**
     * 导出风险类别列表
     */
    @PreAuthorize("@ss.hasPermi('project:riskType:export')")
    @Log(title = "风险类别", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RiskType riskType)
    {
        List<RiskType> list = riskTypeService.selectRiskTypeList(riskType);
        ExcelUtil<RiskType> util = new ExcelUtil<RiskType>(RiskType.class);
        util.exportExcel(response, list, "风险类别数据");
    }

    /**
     * 获取风险类别详细信息
     */
    @PreAuthorize("@ss.hasPermi('project:riskType:query')")
    @GetMapping(value = "/{risktypeId}")
    public AjaxResult getInfo(@PathVariable("risktypeId") Long risktypeId)
    {
        return success(riskTypeService.selectRiskTypeByRisktypeId(risktypeId));
    }

    /**
     * 新增风险类别
     */
    @PreAuthorize("@ss.hasPermi('project:riskType:add')")
    @Log(title = "风险类别", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody RiskType riskType)
    {
        //唯一验证
        Long id =riskTypeService.checkNameUnique(riskType);
        if (id != null){
            return error("现已有风险名称,不能添加");
        }
        return toAjax(riskTypeService.insertRiskType(riskType));
    }

    /**
     * 修改风险类别
     */
    @PreAuthorize("@ss.hasPermi('project:riskType:edit')")
    @Log(title = "风险类别", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody RiskType riskType)
    {
        //唯一验证
        Long id =riskTypeService.checkNameUnique(riskType);
        if (id != null){
            return error("现已有风险名称,不能修改");
        }
        return toAjax(riskTypeService.updateRiskType(riskType));
    }

    /**
     * 删除风险类别
     */
    @PreAuthorize("@ss.hasPermi('project:riskType:remove')")
    @Log(title = "风险类别", businessType = BusinessType.DELETE)
	@DeleteMapping("/{risktypeIds}")
    public AjaxResult remove(@PathVariable Long[] risktypeIds)
    {

//        查询是否外键冲突

        List<Long> fkList = riskTypeService.getFkIds(risktypeIds);
        System.out.println("fkList = " + fkList);

        Long[] ids = Arrays.stream(risktypeIds).filter(id -> !fkList.contains(id)).toArray(Long[]::new);
        System.out.println("ids = " + Arrays.toString(ids));



        if (fkList.size() == risktypeIds.length){
            return error("你选择的所有的风险类别下有项目风险,不能删除");
        }

        if (ids.length < risktypeIds.length) {
            riskTypeService.deleteRiskTypeByRisktypeIds(ids);
            return error("你选择的所有的风险类别下有项目风险,"+fkList+"不能删除");
        }




      return toAjax(riskTypeService.deleteRiskTypeByRisktypeIds(ids));
    }


}
